package cn.vihub.service.impl;

import cn.vihub.handler.MyRuntimeException;
import cn.vihub.param.ShopGoodsAttributeGroupParam;
import cn.vihub.pojo.ShopGoodsAttribute;
import cn.vihub.service.ShopGoodsAttributeService;
import cn.vihub.utils.Result;
import cn.vihub.vo.ShopAttributeGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.mapper.ShopGoodsAttributeGroupMapper;
import cn.vihub.pojo.ShopGoodsAttributeGroup;
import cn.vihub.service.ShopGoodsAttributeGroupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 秋晚
 * @Date:  2025/7/25 20:20
 * @Description: 
 **/

@Service
@Transactional(rollbackFor = Exception.class)
public class ShopGoodsAttributeGroupServiceImpl extends ServiceImpl<ShopGoodsAttributeGroupMapper, ShopGoodsAttributeGroup> implements ShopGoodsAttributeGroupService{

    @Autowired
    ShopGoodsAttributeService shopGoodsAttributeService;
    @Autowired
    ShopGoodsAttributeGroupMapper shopGoodsAttributeGroupMapper;
    @Override
    public IPage<ShopGoodsAttributeGroup> shopGoodsPage(ShopGoodsAttributeGroupParam shopGoodsAttributeGroupParam) {
        //创建分页对象
        Page<ShopGoodsAttributeGroup> shopGoodsAttributeGroupPage = new Page<>(shopGoodsAttributeGroupParam.getPage(), shopGoodsAttributeGroupParam.getLimit());
        //创建lambda表达式
        LambdaQueryWrapper<ShopGoodsAttributeGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return shopGoodsAttributeGroupMapper.selectPage(shopGoodsAttributeGroupPage,lambdaQueryWrapper);
    }

    @Override
    public Integer deleteGroupById(Integer id) {
        //若是该分组下有属性则需先删除属性在进行删除分组
        int count = shopGoodsAttributeService.count(new LambdaQueryWrapper<ShopGoodsAttribute>().eq(ShopGoodsAttribute::getGroupId, id));
        if(count>0){
            throw new MyRuntimeException("该分组下存在属性,请先删除该分组下所有属性后再试!");
        }
        return shopGoodsAttributeGroupMapper.deleteById(id);
    }

    @Override
    public List<ShopAttributeGroupVo> getGroupData() {
        //查询所有分组
        List<ShopGoodsAttributeGroup> groupList = shopGoodsAttributeGroupMapper.selectList(null);

        //查询所有属性
        List<ShopGoodsAttribute> attributes = shopGoodsAttributeService.list();


        ArrayList<ShopAttributeGroupVo> shopAttributeGroupVos = new ArrayList<>();
        //遍历分组集合
        for (ShopGoodsAttributeGroup shopGoodsAttributeGroup : groupList) {
            //将分组对象拷贝给分组Vo对象 用于接收属性集合的值
            ShopAttributeGroupVo shopAttributeGroupVo = new ShopAttributeGroupVo();
            BeanUtils.copyProperties(shopGoodsAttributeGroup,shopAttributeGroupVo);

            ArrayList<ShopGoodsAttribute> attributeList = new ArrayList<>();

            for (ShopGoodsAttribute attribute : attributes) {
                if(Objects.equals(shopAttributeGroupVo.getId(),attribute.getGroupId())){
                    attributeList.add(attribute);
                }
            }
            shopAttributeGroupVo.setShopGoodsAttributeList(attributeList);
            shopAttributeGroupVos.add(shopAttributeGroupVo);
        }

        return shopAttributeGroupVos;
    }
}
